<?php

namespace app\admin\controller\daily;

use think\Db;

class Dianjinggou
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $akikey = 'key=weacb2c7cfcxd5c31a';

    function urlencodess(){
        $url = "https://www.780g.com/wxlogin";
        return urlencode($url);
    }

    /**
     * get
     * @param string $url 请求地址
     */
    function GetHttp(string $url, $header)
    {
        // 关闭句柄
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 0);   //只需要设置一个秒的数量就可以
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl); //返回api的json对象
        if (curl_exec($curl) === false) {
            return 'Curl error: ' . curl_error($curl);
        }
//        dump(curl_getinfo($curl, CURLINFO_HEADER_OUT)); //官方文档描述是“发送请求的字符串”，其实就是请求的header。这个就是直接查看请求header，因为上面允许查看
        //关闭URL请求
        curl_close($curl);
        return json_decode($tmpInfo); //返回json对象
    }

    function writeLog($content)
    {
        $file = 'Dianjinggou.log';
        // 这个函数支持版本(PHP 5)
        if ($f = file_put_contents($file, $content . '\r\n', FILE_APPEND)) {
            //echo "成功。<br />";
        }

    }

    //同步到商品表中
    public function addgoodList()
    {
        $list = DB::name("dianjinggo_goods")->where(['game_id' => 1, 'is_publish' => 1])->select();
        $game_info5 = DB::name("game_info")->where(['gt_id' => 5, 'name' => '账号'])->select();
        $fee_base = DB::name("server")->where(['game_id' => 0, 'safe' => 1, 'method' => 1])->find();
        $fee_list = DB::name("server")->where(['game_id' => ['<>', 0], 'safe' => 1, 'method' => 1])->select();
        try {
            Db::startTrans();
            foreach ($list as $key => $val) {
                $image_arr = explode(',', $val['goodsImages']);
//				$images = '';
//				foreach($image_arr as $image_val){
//					$images .= "https://oss.dianjinghou.com/".$image_val.',';
//				}
//				if($images != '') $images = substr($images,0,-1);
				//dump($image_arr);die;
                $data['goods_class_type'] = $val['goodsTypeId'];
                $id = '1793';
                $rand = $id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
                $sn = date('Yhis') . $rand;
                $id = str_pad($id, (18 - strlen($sn)), '0', STR_PAD_BOTH);
                $order_sn = $sn . $id;
                $data['goods_sn'] = $order_sn;
                $data['uid'] = '1793';
                $data['gameid'] = $val['game_id'];
                $data['gamename'] = $val['game_name'];
                $data['title'] = "【" . $val['f_id'] . "】" . mb_substr($val['bigTitle'], 0, 150);
                $data['image'] = $image_arr[0];
                $data['images']=$val['goodsImages'];
//                $data['image'] = "https://oss.dianjinghou.com/".$image_arr[0];
//                $data['images'] = $images;
                foreach ($game_info5 as $vv) {
                    if ($vv['game_id'] == $val['game_id']) {
                        $data['goods_type'] = $vv['id'];
                        $data['type_name'] = $vv['name'];
                    }
                }
                $data['prices'] = $val['price'];
                //查询费率
                $fee = 0;
                if ($fee_list) {
                    foreach ($fee_list as $vv) {
                        if ($vv['game_id'] == $val['game_id']) {
                            $fee = $vv['service'];
                        }
                    }
                } else {
                    $fee = $fee_base['service'];
                }
                $data['price'] = $val['price'] * (1 + $fee);
                $data['plat_id'] = $val['plat_id'];
                $data['plat'] = $val['plat'];
                $data['operator'] = $val['operator'];
                $data['operator_id'] = $val['operator_id'];
                $data['area_id'] = $val['area_id'];
                $data['area'] = $val['area'];
                $data['server'] = $val['server'];
                $data['server_id'] = $val['server_id'];
                $data['info'] = $val['bigTitle'];
                $data['havetime'] = time() + 86400 * 30; //默认30天
                $data['g_account'] = '';
                $data['g_password'] = '';
                $data['jy_time'] = '3:00-21:00';
//            $data['yd_id'] = '';
                $data['is_warrant'] = 2;
                $data['is_appoint'] = 2;
//            $data['cipher'] = '';
                $data['is_safe'] = 1;
                $data['is_pei'] = 2;
                $data['stock'] = $val['goodsNum'];
                $data['mobile'] = '18868581530';
                $data['qq'] = '2880861415';
                $data['status'] = 1;
                $data['createtime'] = time();
                $res = DB::name("goods")->insertGetID($data);
                if ($res) {
                    DB::name("dianjinggo_goods")->where('id', $val['id'])->update(['is_publish' => 2, 'order_no' => $order_sn]);
                }
            }
            // 提交事务
            Db::commit();
            echo "操作成功！";
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            //注意：我们做了回滚处理
            $this->error($e->getMessage());
        }

    }

    //获取游戏总分类
    public function getGameType()
    {
        //获取用户分类
        $appid = '100135';
        $apiSuffix = '780g';
        $url_first = "http://api.jianghu.fun/api/backstage/dst/outShareGoodsList/" . $apiSuffix;
        $time = time();
        $sign = md5(md5($apiSuffix . $appid) . $time);

        $header = array(
            'timestamp:' . $time,
            'Sign:' . $sign
        );

        //查询本地游戏分类
        $game = DB::name("game")->select();
        $game_info = DB::name("game_info")->select();
        for($i=10;$i<20;$i++){
			echo '$i='.$i;
            $url = $url_first . "?gameId=3&pages=".$i."&pageSize=500";
            $return = $this->GetHttp($url, $header);
			if($return->code == 1){
				dump($return);die;
			}
            $list = $return->data->data;
            if ($list) {
                foreach ($list as $val) {
                    //添加平台
                    $gameName_id = 0;
                    $gamename = $val->gameName;
                    switch ($gamename) {
                        case '魔兽世界':
                            $gamename = '魔兽世界';
                            break;
                        default:
                            break;
                    }
                    foreach ($game as $gameKey => $gameVal) {
                        if ($gameVal['game'] == $gamename) {
                            $gameName_id = $gameVal['id'];
                            break;
                        }
                    }
                    if ($gameName_id == 0) {
                        //本地未找到游戏，加入日志，提示运维加入游戏
                        $this->writeLog("未找到游戏" . $val->gameName);
                        continue;
                    }
                    $data = array();
                    $res = DB::name("dianjinggo_goods")->where('f_id', $val->id)->find();
                    if (!$res) {
                        $data['f_id'] = $val->id;
                        $data['game_id'] = $val->gameId;
                        $data['game_name'] = $gamename;
                        $data['dist_id'] = $val->distId;
                        $data['shop_id'] = $val->shopId;
                        $data['pullId'] = $val->pullId;
                        $data['goodsTypeId'] = $val->goodsTypeId;
                        $data['goodsNum'] = $val->goodsNum;
                        $data['pieceNum'] = $val->pieceNum;
                        $data['imageNum'] = $val->imageNum;
                        $data['thumbnail'] = $val->thumbnail;
                        $data['price'] = $val->price;
                        $data['addPrice'] = $val->addPrice;
                        $data['unitPrice'] = $val->unitPrice;
                        $data['wjj'] = $val->wjj;
                        $data['goodsSn'] = $val->goodsSn;
                        $data['createTime'] = $val->createTime;
                        $data['bigTitle'] = $val->bigTitle;
                        $data['securitService'] = $val->securitService;
                        $data['smallTitle'] = $val->smallTitle;
                        $images = '';
                        if ($val->goodsImages) {
                            foreach ($val->goodsImages as $vals) {
                                $images .= $vals->url . ',';
                            }
                            $images = substr($images, 0, -1);
                        }
                        $data['goodsImages'] = $images;
                        $data['names'] = $val->keyNames;
                        $keyName = json_decode($val->keyNames);
						if(isset($keyName->areaName)){
							$cateType = $keyName->areaName;
							//查找本地游戏分类
							$gameInfoArr = array();
							foreach ($game_info as $gameinfoValue) {
								if ($gameinfoValue['game_id'] == $gameName_id) {
									$gameInfoArr[] = $gameinfoValue;
								}
							}
							//再判断是QQ还是微信
							$gameInfoArr_fenlei1 = array();
							foreach ($gameInfoArr as $gameInfoArr_val) {
								if ($gameInfoArr_val['gt_id'] == 1) {
									$gameInfoArr_fenlei1[] = $gameInfoArr_val;
								}
							}
							$gameInfoArr_fenlei2 = array();
							foreach ($gameInfoArr as $gameInfoArr_val) {
								if ($gameInfoArr_val['gt_id'] == 2) {
									$gameInfoArr_fenlei2[] = $gameInfoArr_val;
								}
							}
							$gameInfoArr_fenlei3 = array();
							foreach ($gameInfoArr as $gameInfoArr_val) {
								if ($gameInfoArr_val['gt_id'] == 3) {
									$gameInfoArr_fenlei3[] = $gameInfoArr_val;
								}
							}
							$gameInfoArr_fenlei4 = array();
							foreach ($gameInfoArr as $gameInfoArr_val) {
								if ($gameInfoArr_val['gt_id'] == 4) {
									$gameInfoArr_fenlei4[] = $gameInfoArr_val;
								}
							}
							$gameInfoArr_fenlei5 = array();
							foreach ($gameInfoArr as $gameInfoArr_val) {
								if ($gameInfoArr_val['gt_id'] == 5) {
									$gameInfoArr_fenlei5[] = $gameInfoArr_val;
								}
							}
							//判断平台 (安卓、苹果)
							if (preg_match('/安卓/', $cateType)) {
								foreach ($gameInfoArr_fenlei1 as $fenlei_val) {
									if (preg_match('/安卓/', $fenlei_val['name'])) {
										$data['plat_id'] = $fenlei_val['id'];
									}
								}

								if (preg_match('/QQ/', $cateType)) {
									foreach ($gameInfoArr_fenlei2 as $fenlei_val) {
										if (preg_match('/QQ/', $fenlei_val['name'])) {
											$data['operator_id'] = $fenlei_val['id'];
											$data['operator'] = $fenlei_val['name'];
											$data['area'] = $cateType;
											$data['server'] = '全服';
										}
									}
								} else if (preg_match('/微信/', $cateType)) {
									foreach ($gameInfoArr_fenlei2 as $fenlei_val) {
										if (preg_match('/微信/', $fenlei_val['name'])) {
											$data['operator_id'] = $fenlei_val['id'];
											$data['operator'] = $fenlei_val['name'];
											$data['area'] = $cateType;
											$data['server'] = '全服';
										}
									}
								}

							} else if (preg_match('/苹果/', $cateType)) {
								foreach ($gameInfoArr_fenlei1 as $fenlei_val) {
									if (preg_match('/苹果/', $fenlei_val['name'])) {
										$data['plat_id'] = $fenlei_val['id'];
									}
								}
								if (preg_match('/QQ/', $cateType)) {
									foreach ($gameInfoArr_fenlei2 as $fenlei_val) {
										if (preg_match('/QQ/', $fenlei_val['name'])) {
											$data['operator_id'] = $fenlei_val['id'];
											$data['operator'] = $fenlei_val['name'];
											$data['area'] = $cateType;
											$data['server'] = '全服';
										}
									}
								} else if (preg_match('/微信/', $cateType)) {
									foreach ($gameInfoArr_fenlei2 as $fenlei_val) {
										if (preg_match('/微信/', $fenlei_val['name'])) {
											$data['operator_id'] = $fenlei_val['id'];
											$data['operator'] = $fenlei_val['name'];
											$data['area'] = $cateType;
											$data['server'] = '全服';
										}
									}
								}
							} else if (preg_match('/官服/', $cateType)) {
								if ($gameInfoArr_fenlei1) {
									foreach ($gameInfoArr_fenlei1 as $fenlei_val) {
										if (preg_match('/双平台互通/', $fenlei_val['name'])) {
											$data['plat_id'] = $fenlei_val['id'];
										}
									}
								} else {
									foreach ($gameInfoArr_fenlei4 as $fenlei_val) {
										if (preg_match('/官方/', $fenlei_val['name'])) {
											$data['area_id'] = $fenlei_val['id'];
											$data['area'] = $fenlei_val['name'];
										}
									}
								}

							} else if (preg_match('/电信/', $cateType)) {
								foreach ($gameInfoArr_fenlei4 as $fenlei_val) {
									if (preg_match('/电信/', $fenlei_val['name'])) {
										$data['server_id'] = $fenlei_val['id'];
										$data['server'] = $fenlei_val['name'];
									}
								}
							} else if (preg_match('/联通/', $cateType)) {
								foreach ($gameInfoArr_fenlei4 as $fenlei_val) {
									if (preg_match('/联通/', $fenlei_val['name'])) {
										$data['server_id'] = $fenlei_val['id'];
										$data['server'] = $fenlei_val['name'];
									}
								}
							} else if (preg_match('/Steam/', $cateType)) {
								if ($gameInfoArr_fenlei1) {
									foreach ($gameInfoArr_fenlei4 as $fenlei_val) {
										if (preg_match('/steam/', $fenlei_val['name'])) {
											$data['plat_id'] = $fenlei_val['id'];
										}
									}
								} else {
									foreach ($gameInfoArr_fenlei4 as $fenlei_val) {
										if (preg_match('/steam/', $fenlei_val['name'])) {
											$data['area_id'] = $fenlei_val['id'];
											$data['area'] = $fenlei_val['name'];
										}
									}
								}

							} else {
								$data['plat_id'] = 0;
								foreach ($gameInfoArr_fenlei1 as $fenlei_val) {
									if (preg_match('/' . $cateType . '/', $fenlei_val['name'])) {
										$data['plat_id'] = $fenlei_val['id'];
									}
								}
							}
							$data['server'] = $keyName->areaName;
						}

                        if (isset($keyName->accountTypeNames)) {
                            $data['accountTypeNames'] = $keyName->accountTypeNames;
                        }
                        if (isset($keyName->areaName)) {
                            $data['area'] = $keyName->areaName;
                        }
                        if (isset($keyName->crossName)) {
                            $data['cross'] = $keyName->crossName;
                        }
                        

                        $data['values'] = $val->keyValues;
                        $data['created_time'] = date("Y-m-d H:i:s", time());
                        DB::name("dianjinggo_goods")->insert($data);
                    }
                }
                dump($return);
            } else {
                echo 'empty';
                dump($return);
            }
	}
        echo '执行成功';
        die;
    }

    public function getArea()
    {
        $url = "http://aaa.dianjinghou.top/api/comm/getAreaServerList?gameId=3";
        $return = $this->GetHttp($url, array());
        dump($return);
        die;
    }
	
	public function getCross(){
		$list = DB::name("goods")->where(['id'=>['>','5860'],'gameid'=>2,'gamename'=>'地下城与勇士','jy_time'=>'3:00-21:00'])->select();
		foreach($list as $val){
			$info = DB::name("dianjinggo_goods")->where("order_no",$val['goods_sn'])->find();
			if($info){
				$data = array(
					'cross_id'=>$info['cross_id'],
					'cross'=>$info['cross_name']
				);
				DB::name('goods')->where("id",$val['id'])->update($data);
			}
			
		}
		echo '成功';
		dump($list);die;
	}

	public function updateCreateTime(){
        $list = DB::name("fa_goods")->where(['uid'=>'1793','mobile'=>'18868581530','status'=>1])->select();
        foreach($list as $val){
            DB::name("fa_goods")->where("id",$val['id'])->update(['createtime'=>time()]);
        }
    }
}